import numpy as np
import pandas as pd


bmi_life_data = pd.read_csv('bmi_and_life_expectancy.csv')


x = np.array(bmi_life_data[["BMI"]])
y = np.array(bmi_life_data["Life expectancy"])

epochs = 1000
learning_rate = 0.001


def linear_regression(x, y):
    w = 1
    b = 0
    for epoch in range(epochs):
        for i in range(len(x)):
            y_hat = w * x[i] + b
            delt = learning_rate * (y[i] - y_hat)

            w += w * delt
            b += delt
    return w, b

def predict(w, b, bmi):
    return w[0] * bmi + b[0]


w, b = linear_regression(x, y)

predBMI = 21.07931
output = predict(w, b, predBMI)


print("parameters after train, w: %f, b: %f" %(w, b))

print("test BMI: %lf output life expectancy: %lf" %(predBMI, output))